package com.example.demo.generator.controller;

import com.example.demo.common.BaseResponse;
import com.example.demo.common.ResultUtils;
import com.example.demo.generator.codegen.exception.SqlParseException;
import com.example.demo.generator.codegen.model.ClassInfo;
import com.example.demo.generator.dto.SqlParseRequest;
import com.example.demo.generator.service.CodeGeneratorFacade;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Minimal REST endpoint that exposes SpringBootCodeGenerator parsing as a template feature.
 */
@RestController
@RequestMapping("/generator")
@Validated
@Tag(name = "代码生成预览", description = "基于 SpringBootCodeGenerator 的 SQL 元数据解析")
public class CodeGeneratorController {

    private final CodeGeneratorFacade codeGeneratorFacade;

    public CodeGeneratorController(CodeGeneratorFacade codeGeneratorFacade) {
        this.codeGeneratorFacade = codeGeneratorFacade;
    }

    /**
     * 使用 SpringBootCodeGenerator 解析 SQL，并返回 ClassInfo 元数据。
     *
     * @param request SQL 解析请求
     * @return ClassInfo 元数据
     */
    @PostMapping("/preview")
    @Operation(summary = "SQL 解析预览", description = "根据 SQL 类型返回字段元数据，便于脚手架后续生成代码。")
    public BaseResponse<ClassInfo> preview(@Valid @RequestBody SqlParseRequest request) throws SqlParseException {
        ClassInfo result = codeGeneratorFacade.previewSql(request);
        return ResultUtils.success(result);
    }
}
